package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client2 {
    private Socket socket;
    public Client2(){
        try {
            System.out.println("正在连接服务器");
            socket =new Socket("localhost",8877);
            System.out.println("连接成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void satay()  {
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter opw=new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw=new BufferedWriter(opw);
            PrintWriter pw=new PrintWriter(bw,true);
            Scanner scan=new Scanner(System.in);
            while (true){
                String aat=scan.nextLine();
                if ("exit".equals(aat)){
                    break;
                }
                pw.println(aat);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    public static void main(String[] args) {
        Client2 client2=new Client2();
        client2.satay();
    }
}
